const router = require('koa-router')()
const { Layer } = require('koa-router');
const mysql = require('mysql');
const { query } = require('../public/javascripts/db')
// 创建数据库链接

let options = {};
router.post('/group', async (ctx, next) => {
    let { group_name, people_count, text, foradd } = ctx.request.body
    let sqls = 'insert into sgroup(group_name,people_count,text,foradd) value(?,?,?,?)';
    await query(sqls, [group_name, people_count, text, foradd]);
    ctx.body = '创建小组的相关信息'
})
router.get('/getgroup', async (ctx, next) => {
    let sql = 'select * from sgroup where foradd=0';
    let sqls = "select * from sgroup where foradd =1";
    let groupList;
    let num = 0;
    let haveList;
    await query(sql, []).then(res => {
        options.groupList = res;
    });
    await query(sqls, []).then(res => {
        options.num = res
    });
    await query(sqls, []).then(res => {
        options.haveList = res
    });
    haveList = options.haveList;
    num = options.num;
    let max = Math.max.call(0, num);
    groupList = options.groupList;
    let newarr = getArrayItems(groupList, 5);
    ctx.body = { "groupList": newarr, "num": max, "haveList": haveList };
})

function getArrayItems(arr, num) {
    //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
    var temp_array = new Array();
    for (var index in arr) {
        temp_array.push(arr[index]);
    }
    //取出的数值项,保存在此数组
    var return_array = new Array();
    for (var i = 0; i < num; i++) {
        //判断如果数组还有可以取出的元素,以防下标越界
        if (temp_array.length > 0) {
            //在数组中产生一个随机索引
            var arrIndex = Math.floor(Math.random() * temp_array.length);
            //将此随机索引的对应的数组元素值复制出来
            return_array[i] = temp_array[arrIndex];
            //然后删掉此索引的数组元素,这时候temp_array变为新的数组
            temp_array.splice(arrIndex, 1);
        } else {
            //数组中数据项取完后,退出循环
            break;
        }
    }
    arr = return_array;
    return arr;
}
router.post('/put', async (ctx, next) => {
    let putList;
    let { putid } = ctx.request.body;
    let sqlss = "select * from sgroup where group_id=?";
    await query(sqlss, [putid]).then(res => {
        options.putList = res;
    })
    putList = options.putList;
    ctx.body = { "confirms": putList };
})
router.post('/puts', async (ctx, next) => {
    let putids = ctx.request.body.putids
    let sqls = 'UPDATE sgroup SET foradd=1 WHERE foradd=0 and group_id=?';
    let sql = "update sgroup set people_count=people_count+1 where group_id=? and foradd=0";
    await query(sql, [putids]);
    await query(sqls, [putids]);
    ctx.body = '通过邀请码加入小组'
})
router.post('/update', async (ctx, next) => {
    let newid = ctx.request.body.newid
    let sqls = 'UPDATE sgroup SET foradd=1 WHERE foradd=0 and group_id=?';
    let sql = "update sgroup set people_count=people_count+1 where group_id=?";
    await query(sqls, [newid]);
    await query(sql, [newid]);
    ctx.body = '更新加入状态和小组人数'
})
router.post('/quit', async (ctx, next) => {
    let quitid = ctx.request.body.quitid
    let sql = 'UPDATE sgroup SET foradd=0 WHERE foradd=1 and group_id=?';
    let sqls = "update sgroup set people_count=people_count-1 where group_id=?";
    await query(sql, [quitid]);
    await query(sqls, [quitid]);
    ctx.body = '退出小组'
})

module.exports = router